﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.ComponentModel;

namespace EkspSys.Components
{
    class ApplicationListItem : IListItem
    {
        private Process process;
        private Color C;

        public ApplicationListItem(Process process, Color c)
        {
            this.process = process;
            C = c;
        }

        public Process Process
        {
            get
            {
                return process;
            }
        }

        public string GetName()
        {
            return process.ProcessName;
        }



        public Icon GetIcon()
        {
            try
            {
                return Icon.ExtractAssociatedIcon(process.MainModule.FileName);
            }
            catch (Win32Exception ex)
            {
                return global::EkspSys.Properties.Resources.Blank;
                Console.WriteLine("Icon gettter fejler {0}", ex);
            }
        }

        public Color GetColor()
        {
            return C;
        }

        private const int ItemMargin = 5;
        private const int PictureHeight = 25;

        public void Draw(DrawItemEventArgs e)
        {

            // See if the item is selected.
            Brush br;
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                br = SystemBrushes.HighlightText;
            else
                br = new SolidBrush(GetColor());

            // Draw the picture.
            float scale = PictureHeight;
            int picture_width = PictureHeight;
            Rectangle dest_rect = new Rectangle(e.Bounds.Left + ItemMargin, e.Bounds.Top + ItemMargin, picture_width, PictureHeight);

            // Draw the background.
            e.DrawBackground();

            Pen p = new Pen(GetColor());
            e.Graphics.DrawIcon(GetIcon(), dest_rect);

            // Find the area in which to put the text.
            float x = e.Bounds.Left + picture_width + 3 * ItemMargin;
            float y = e.Bounds.Top + ItemMargin;
            float width = e.Bounds.Right - ItemMargin - x;
            float height = e.Bounds.Bottom - ItemMargin - y;
            RectangleF layout_rect = new RectangleF(x, y, width, height);

            // Draw the text.
            string txt = GetName();
            Font f = new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold);
            e.Graphics.DrawString(txt, f, br, layout_rect);

            // Draw the focus rectangle if appropriate.
            e.DrawFocusRectangle();
        }

        public void PerformAction(MainWindow win)
        {
            win.ShowOverlayForProcess(Process);
        }
    }
}
